<template>
    <div class="more-setting">
        <a-card class="card">
            <div class="card-item">
                <h3>谁能发起</h3>
                <div class="flex items-center">
                    <a-input :value="state.setting?.wfWorkFlowSendGroup?.senderNames" class="w-full" @click="handleSetSender" readonly/>
                </div>
            </div>
        </a-card>
        <select-dept-role-user ref="selectOrgRoleUserRef" @selected="getSelected" :is-show-dept="true" :is-show-role="false" :is-show-user="false"/>
    </div>
</template>

<script setup lang="ts">
import { reactive, useTemplateRef } from 'vue';
import selectDeptRoleUser from '@/components/select-dept-role-user/index.vue'

const selectOrgRoleUserRef = useTemplateRef('selectOrgRoleUserRef')
const state = reactive({
    setting:{
        wfWorkFlowSendGroup:{
            senderNames:"",
            orgIds:[]  as any[],
            orgList:[] as any[],
            roleIds:[] as any,
            roleList:[] as any[],
            userIds:[] as any,
            userList:[] as any[],
            selected:[] as any[],
        },
    },
})
const handleSetSender = ()=>{
    selectOrgRoleUserRef.value?.handleOpen(state.setting?.wfWorkFlowSendGroup?.selected)
}

/**
 * 只处理部门权限发起人
 * @param v 
 */
const getSelected  = (v:any)=>{
    state.setting.wfWorkFlowSendGroup.selected = v
    state.setting.wfWorkFlowSendGroup.orgIds = []
    state.setting.wfWorkFlowSendGroup.roleIds = []
    state.setting.wfWorkFlowSendGroup.userIds = []
    state.setting.wfWorkFlowSendGroup.orgList = []
    state.setting.wfWorkFlowSendGroup.roleList = []
    state.setting.wfWorkFlowSendGroup.userList = []
    let name :string[]= []
    state.setting.wfWorkFlowSendGroup.selected.forEach((item:any)=>{
        name.push(item.name)
        if(item.type == 'dept'){
            state.setting.wfWorkFlowSendGroup.orgIds.push(item.id)
            state.setting.wfWorkFlowSendGroup.orgList.push(item)
        }else if (item.type == 'role'){
            state.setting.wfWorkFlowSendGroup.roleIds.push(item.id)
            state.setting.wfWorkFlowSendGroup.roleList.push(item)
        }else if (item.type == 'user'){
            state.setting.wfWorkFlowSendGroup.userIds.push(item.id)
            state.setting.wfWorkFlowSendGroup.userList.push(item)
        }
    })
    state.setting.wfWorkFlowSendGroup.senderNames = name.join(',')
    console.log(state.setting);
    
}

const getMoreSetting = ()=>{
    return state.setting
}

const setMoreSetting = (data:any)=>{
    if(data != null){
        state.setting = data
    } 
}

defineExpose({
    getMoreSetting,
    setMoreSetting
})
</script>

<style lang='scss' scoped>
.more-setting{
    display: flex;
    justify-content: center;
    height: 100%;
    .card{
        width: 40%;
        height: 100%;
        padding: 25px;
    }
}
</style>